# 初始化棋盘
board = [' ' for _ in range(9)]

# 打印棋盘
def print_board(board):
    print(f"{board[0]}|{board[1]}|{board[2]}")
    print("-+-+-")
    print(f"{board[3]}|{board[4]}|{board[5]}")
    print("-+-+-")
    print(f"{board[6]}|{board[7]}|{board[8]}")

# 检查玩家是否获胜
def check_win(board, player):
    win_combinations = [
        [0, 1, 2], [3, 4, 5], [6, 7, 8],  # 行
        [0, 3, 6], [1, 4, 7], [2, 5, 8],  # 列
        [0, 4, 8], [2, 4, 6]              # 对角线
    ]
    for combination in win_combinations:
        if all(board[i] == player for i in combination):
            return True
    return False

# 检查棋盘是否已满
def is_full(board):
    return ' ' not in board

# 主游戏循环
def main():
    current_player = 'X'
    while True:
        print_board(board)
        try:
            move = int(input(f"玩家 {current_player}，请输入 0 - 8 的位置："))
            if 0 <= move <= 8 and board[move] == ' ':
                board[move] = current_player
                if check_win(board, current_player):
                    print_board(board)
                    print(f"玩家 {current_player} 获胜！")
                    break
                elif is_full(board):
                    print_board(board)
                    print("平局！")
                    break
                current_player = 'O' if current_player == 'X' else 'X'
            else:
                print("无效的位置，请重新输入。")
        except ValueError:
            print("输入无效，请输入一个 0 - 8 的整数。")

if __name__ == "__main__":
    main()